package com.fairytask.engine.components
{
	import com.fairytask.data.BaseData;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class BaseItem extends MovieClip
	{
		private var _data		:BaseData	=	new BaseData();
		
		private var _callBack	:Function	=	null;
		
		private var _eventArr	:BaseData	=	new BaseData();
		
		public var skinMc		:MovieClip;
		private var _itemSpace	:Sprite	=	new Sprite();
		private var _itemContent:DisplayObject;
		
		public function BaseItem(_skin:String="",pw:int=0,ph:int=0)
		{
			super();
			if(_skin!=""&&null!=_skin)
			{
				skinMc	=	ResManager.newResClass(_skin) as MovieClip;
				addChild(skinMc);
				addChild(_itemSpace);
				if(pw>0)
				{
					skinMc.width		=	pw;
					skinMc.height	=	ph;
				}
			}
		}
		
		public function addItemEvent(type:String,listener:Function,data:Object=null):void
		{
			this.addEventListener(type,onEvent);
			_eventArr.HandleProperties(type,{event:listener,data:data});
		}
		
		public function removeItemEvent(type:String,listener:Function):void
		{
			this.removeEventListener(type,onEvent);
			_eventArr.DeleteProperties(type);
		}
		
		public function clearEvent():void
		{
			_eventArr.getDataForEach(function(key:String,val:*):void
			{
				this.removeEventListener(key,onEvent);
				_eventArr.DeleteProperties(key);
			});
		}
		
		public function clear():void
		{
			while(_itemSpace.numChildren>0)
			{
				_itemSpace.removeChildAt(0);
			}
			_itemContent	=	null;
		}
		
		/**
		 * 添加额外显示对象 
		 * @param val
		 * 
		 */
		public function addItem(val:DisplayObject):void
		{
			_itemContent	=	val;
			_itemSpace.addChild(val);
		}
		public function removeItem(val:DisplayObject):void
		{
//			_itemContent	=	val;
			_itemSpace.removeChild(val);
		}
		public function getItem():DisplayObject
		{
			return _itemContent;
		}
		
		private function onEvent(e:Event):void
		{
			var tmpEvent:Object	=	_eventArr.GetProperties(e.type);
			_callBack			=	tmpEvent.event;
			_callBack.call(this,e,tmpEvent.data);
		}

		public function get data():BaseData
		{
			return _data;
		}

		public function set data(value:BaseData):void
		{
			_data = value;
		}

	}
}